#define  _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"

//int main()
//{
//	ST s;
//	STInit(&s);
//	STPush(&s, 1);
//	STPush(&s, 2);
//	STPush(&s, 3);
//	STPush(&s, 4);
//	STPush(&s, 5);
//
//	while (!STEmpty(&s))
//	{
//		printf("%d ", STTop(&s));
//		STPop(&s);
//	}
//
//	STDestroy(&s);
//	return 0;
//}


void swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

int GetMid(int* a, int left, int right)
{
	int mid = (left + right) / 2;
	if (a[mid] > a[left])
	{
		if (a[right] > a[mid])
			return mid;
		else
			return a[left] > a[right] ? left : right;
	}
	else
	{
		if (a[mid] > a[right])
			return mid;
		else
			return a[left] > a[right] ? right : left;
	}
}


int PartSort(int* a, int left, int right)
{
	if (left > right)
		return;
	int index = GetMid(a, left, right);
	swap(&a[left], &a[index]);
	int begin = left;
	int end = right;
	int key = left;
	while (begin < end)
	{
		while (begin < end && a[begin] < a[key])
			begin++;
		while (begin < end && a[end] > a[key])
			end--;
		swap(&a[begin], &a[end]);
	}
	swap(&a[end], &a[key]);
	return end;
}

void QuickSortNonR(int* a, int n)
{
	ST st;
	STInit(&st);
	STPush(&st, 0);
	STPush(&st, n-1);
	while (!STEmpty(&st))
	{
		int right = STTop(&st);
		STPop(&st);
		int left = STTop(&st);
		STPop(&st);
		int index = PartSort(a, left, right);
		
		if (left < index - 1)
		{
			STPush(&st, left);
			STPush(&st, index - 1);
		}
		if (index + 1 < right)
		{
			STPush(&st, index + 1);
			STPush(&st, right);
		}

	}
	STDestroy(&st);
}


void TestQSortNonR()
{
	//int a[] = { 1,2,3,4,5,6,7,8,9 };
	int a[] = { 12,42,54,65,16,24,26,27,52,7,2,1,0,6,8 };
	int sz = sizeof(a) / sizeof(int);
	QuickSortNonR(a, sz);
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void _MergeSort(int* a, int left, int right, int* tmp)
{
	if (left > right)
		return;
	int mid = (left + right) / 2;
	_MergeSort(a, left, mid, tmp);
	_MergeSort(a, mid+1, right, tmp);

	int begin1 = left;
	int end1 = mid;
	int begin2 = mid + 1;
	int end2 = right;
	int index = left;
	while(begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] > a[begin2])
			tmp[index++] = a[begin2++];
		else
			tmp[index++] = a[begin1++];
	}
	while (begin1 <= end1)
		tmp[index++] = a[begin1++];
	while (begin2 <= end2)
		tmp[index++] = a[begin2++];
	for (int i = left; i <= right; i++)
	{
		a[i] = tmp[i];
	}
}

void MergeSort(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	_MergeSort(a, 0, n - 1, tmp);
}


void MergeSortNonR(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	for (int gap = 1; gap < n; gap *= 2)
	{
		for (int i = 0; i < n; i+= 2*gap)
		{
			int begin1 = i;
			int end1 = i + gap - 1;
			int begin2 = i + gap;
			int end2 = i + 2 * gap - 1;
			int index = i;
			if (begin2 > n)
				break;
			if (end2 >= n)
				end2 = n - 1;
			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] > a[begin2])
					tmp[index++] = a[begin2++];
				else
					tmp[index++] = a[begin1++];
			}
			while (begin1 <= end1)
				tmp[index++] = a[begin1++];
			while (begin2 <= end2)
				tmp[index++] = a[begin2++];
			for (int j = i; j <= end2; j++)
			{
				a[j] = tmp[j];
			}
		}
	}
}

void TestMergeSort()
{
	//int a[] = { 1,2,3,4,5,6,7,8,9 };
	int a[] = { 12,42,54,65,16,24,26,27,52,7,2,1,0,6,8 };
	int sz = sizeof(a) / sizeof(int);
	MergeSort(a, sz);
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void TestMergeSortNonR()
{
	//int a[] = { 1,2,3,4,5,6,7,8,9 };
	int a[] = { 12,42,54,65,16,24,26,27,52,7,2,1,0,6,8 };
	int sz = sizeof(a) / sizeof(int);
	MergeSortNonR(a, sz);
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

int main()
{
	TestMergeSortNonR();
	return 0;
}